package com.ruoyi.mtAudit.controller;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.domain.MateCheck;
import com.ruoyi.mateCheck.service.MateCheckService;
import com.ruoyi.mateInventory.domain.MateInventory;
import com.ruoyi.mateInventory.service.MateInventoryService;
import com.ruoyi.mateproduction.domain.MateCleanRecord;
import com.ruoyi.mateproduction.domain.MateDaily;
import com.ruoyi.mateproduction.domain.MateProductionPlan;
import com.ruoyi.mateproduction.domain.MateProductionRecord;
import com.ruoyi.mateproduction.domain.MateTransfer;
import com.ruoyi.mateproduction.domain.MtCourseCheck;
import com.ruoyi.mateproduction.service.MateCleanRecordService;
import com.ruoyi.mateproduction.service.MateCourseService;
import com.ruoyi.mateproduction.service.MateDailyService;
import com.ruoyi.mateproduction.service.MateTransferService;
import com.ruoyi.mateproduction.service.MtProductionRecordService;
import com.ruoyi.mateproduction.service.MtProductionService;
import com.ruoyi.mateRequistion.domain.MateRequistion;
import com.ruoyi.mateRequistion.service.MateRequistionService;
import com.ruoyi.mateThrough.domain.MateThrough;
import com.ruoyi.mateThrough.service.MateThroughService;
import com.ruoyi.material.domain.MateEvaluate;
import com.ruoyi.material.domain.MateProductCheck;
import com.ruoyi.material.service.CheckReportService;
import com.ruoyi.material.service.MateManufactuterService;
import com.ruoyi.material.service.MateProductService;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import com.ruoyi.mtAudit.service.MtAuditService;
import com.ruoyi.productCheck.domain.CheckInformation;
import com.ruoyi.productCheck.domain.ProductReport;
import com.ruoyi.productCheck.mapper.CheckInformationMapper;
import com.ruoyi.productCheck.mapper.ProductReportMapper;
import com.ruoyi.readyProcurement.domain.ReadyProcurement;
import com.ruoyi.readyProcurement.service.ReadyService;
import io.swagger.models.auth.In;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.apache.poi.xwpf.usermodel.*;

import javax.lang.model.element.Element;
import javax.lang.model.util.Elements;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mtAudit")
public class MtAuditController extends BaseController {
    @Autowired
    MtAuditService mtAuditService;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MateRequistionService mateRequistionService;

    @Autowired
    MateCheckService mateCheckService;

    @Autowired
    MateProductService mateProductService;

    @Autowired
    MateInventoryService mateInventoryService;

    @Autowired
    MateManufactuterService mateManufactuterService;

    @Autowired
    MateThroughService mateThroughService;

    @Autowired
    CheckReportService checkReportService;

    @Autowired
    MtProductionService mtProductionService;

    @Autowired
    MtProductionRecordService mtProductionRecordService;

    @Autowired
    MateDailyService mateDailyService;

    @Autowired
    MateCourseService mateCourseService;

    @Autowired
    MateTransferService mateTransferService;

    @Autowired
    MateCleanRecordService mateCleanRecordService;

    @Autowired
    CheckInformationMapper checkInformationMapper;

    @Autowired
    ProductReportMapper productReportMapper;

    @Autowired
    ReadyService readyService;

    @GetMapping("/selectEdit/{auditId}")
    @ResponseBody
    public Map<String, Object> edit(@PathVariable("auditId") Long auditId) {
        Map<String, Object> resMap = new HashMap<>();
        MtAudit mtAudit = mtAuditService.selectAuditById(auditId);
        resMap.put("auditId", auditId);
        resMap.put("auditStatus", mtAudit.getAuditStatus());
        resMap.put("auditRemark", mtAudit.getAuditRemark());
        String type = mtAudit.getOrderType();

        if ("0".equals(type)) {
            MateCheck mateCheck = mateCheckService.selectCheckInformationById(mtAudit.getOrderId());
            resMap.put("mateCheck", mateCheck);
            resMap.put("userName", mtAudit.getUserName());

        } else if ("1".equals(type)) {
            MateProductCheck mateProductCheck = checkReportService.selectMateProductCheckById(mtAudit.getOrderId());
            JSONArray jsonArray = new JSONArray(Collections.singletonList(mateProductCheck.getInformationList()));
            mateProductCheck.setOtherInformation(jsonArray);
            resMap.put("mateProductCheck", mateProductCheck);

        } else if ("3".equals(type)) {
            MateInventory mateInventory = mateInventoryService.selectEdit(mtAudit.getOrderId());
            resMap.put("mateInventory", mateInventory);

        } else if ("4".equals(type)) {
            Map<String, Object> map = new HashMap<>();
            resMap.put("evaluateId", auditId);
            resMap.put("deptId", SecurityUtils.getDeptId());
            MateEvaluate mateEvaluate = mateManufactuterService.selectEdit(map);
            resMap.put("mateEvaluate", mateEvaluate);

        } else if ("5".equals(type)) {
            MateRequistion mateRequistion = mateRequistionService.selectMateRequistion(mtAudit.getOrderId());
            resMap.put("mateRequistion", mateRequistion);

        } else if ("6".equals(type)) {
            MateThrough mateThrough = mateThroughService.selectMateThrough(mtAudit.getOrderId());
            resMap.put("mateThrough", mateThrough);

        } else if ("7".equals(type)) {
            MateProductionPlan mateProductionPlan = mtProductionService.selectPlan(mtAudit.getOrderId());
            resMap.put("result", mateProductionPlan);

        } else if ("8".equals(type)) {
            MateProductionRecord mateProductionRecord = mtProductionRecordService.selectRecord(mtAudit.getOrderId());
            resMap.put("result", mateProductionRecord);

        } else if ("9".equals(type)) {
            MateDaily mateDaily = mateDailyService.selectMateDaily(mtAudit.getOrderId());
            resMap.put("result", mateDaily);

        } else if ("10".equals(type)) {
            MtCourseCheck mtCourseCheck = mateCourseService.selectMateCourse(mtAudit.getOrderId());
            resMap.put("result", mtCourseCheck);

        } else if ("11".equals(type)) {
            MateTransfer mateTransfer = mateTransferService.selectMateTransfer(mtAudit.getOrderId());
            resMap.put("result", mateTransfer);

        } else if ("12".equals(type)) {
            MateCleanRecord mateCleanRecord = mateCleanRecordService.selectCleanRecord(mtAudit.getOrderId());
            resMap.put("result", mateCleanRecord);

        } else if ("13".equals(type)) {
            CheckInformation checkInformation = checkInformationMapper.selectInformation(mtAudit.getOrderId());
            resMap.put("result", checkInformation);

        } else if ("14".equals(type)) {
            ProductReport productReport = productReportMapper.selectReport(mtAudit.getOrderId());
            resMap.put("result", productReport);

        } else if ("16".equals(type)) {
            ReadyProcurement readyProcurement = readyService.selectReadyProcurement(mtAudit.getOrderId());
            resMap.put("readyProcurement", readyProcurement);

        }
        return resMap;
    }

    @RequiresPermissions("business:audit:edit")
    @Transactional(rollbackFor = Exception.class)
    @Log(title = "订单审核", businessType = BusinessType.UPDATE)
    @GetMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam(value = "auditId", required = false) Long auditId,
                               @RequestParam(value = "auditStatus", required = false) String auditStatus,
                               @RequestParam(value = "orderType", required = false) String orderType) {
        MtAudit mtAudit = new MtAudit();
        mtAudit.setAuditId(auditId);
        mtAudit.setAuditStatus(auditStatus);
        mtAudit.setOrderType(orderType);
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        MtAudit mtAudit1 = mtAuditService.selectAuditByIdIn(mtAudit);
        if (mtAudit1.getAuditStatus().equals("1") || mtAudit1.getAuditStatus().equals("2")) {
            mtAudit.setAuditStatus(mtAudit1.getAuditStatus());
            return AjaxResult.error("该订单已经审核完成");
        }
        Long deptId = sysUser.getDeptId();
        int i = mtAuditService.updateAudit(mtAudit, deptId);
        return toAjax(i);
    }

    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/returnReview/{auditId}")
    @ResponseBody
    public int returnReview(@PathVariable("auditId") Long auditId) {
        MtAudit mtAudit = new MtAudit();
        mtAudit.setAuditId(auditId);
        MtAudit audit1 = mtAuditService.selectAuditByIdIn(mtAudit);
        if (audit1.getAuditStatus().equals("0")) {
            return 2;
        }
        int i = mtAuditService.returnReview(auditId);
        if (i != 0) {
            return 1;
        } else {
            return 2;
        }
    }

    /**
     * 查询订单审核列表
     */
    @RequiresPermissions("business:audit:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(value = "auditStatus", required = false) String auditStatus,
                              @RequestParam(value = "orderType", required = false) String orderType) {
        MtAudit mtAudit = new MtAudit();
        startPage();
        Long companyId = SecurityUtils.getLoginUser().getCompanyId();
        mtAudit.setCompanyId(companyId);
        mtAudit.setAuditStatus(auditStatus);
        mtAudit.setOrderType(orderType);
        List<MtAudit> list = mtAuditService.selectAuditList(mtAudit);
        return getDataTable(list);
    }

    public static void main(String[] args) {
        try {
            XWPFDocument document = new XWPFDocument();
            List<Integer> list=new ArrayList<>();
            list.add(1);
            list.add(2);
            // 创建一个表格
            XWPFTable table = document.createTable(list.size(), 3);//前面的是行,后面的是列

            // 在表格中填充数据
            for (int i = 0; i < list.size(); i++) {
                for (int j = 0; j < 3; j++) {
                    XWPFTableCell cell = table.getRow(i).getCell(j);
                    cell.setText("Row " + i + ", Col " + j);
                }
            }

            // 保存文档
            String filePath = "E:\\ceshi\\DynamicTable.docx";
            FileOutputStream out = new FileOutputStream(filePath);
            document.write(out);
            out.close();
            System.out.println("Word 表格创建成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
